import { Order } from '../../../module/order.js'

const api = new Order()
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {},
    goods: {},
    password: '',
    shop_name:'',
    qrcode: '',
    shops: [],
    use_num: 0,
    post_loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let order_id = options.id;
    if (!order_id) {
      app.toast('参数错误');
      return false;
    }
    this.getOrder(order_id);
  },

  getOrder (id) {
    api.verificationOrder(id).then((data) => {
      let use_num = 1;
      if (data.goods.multiple_use != 1) {
        use_num = data.order.total;
      }
      this.setData({
        goods: data.goods,
        order: data.order,
        use_num: use_num
      });
    }).catch((err) => {
      app.toast(err);
    });
  },

  onChange: function (e) {
    let password = e.detail;
    this.setData({
      password: password
    });
  },
  onNameChange: function (e) {
    let shop_name = e.detail;
    this.setData({
      shop_name: shop_name
    });
  },

  onNumChange: function(e) {
    this.setData({
      use_num: e.detail
    })
  },

  doUse: function () {
    if (!this.data.password) {
      app.toast('请输入核销密码');
      return false;
    }
    if (!this.data.shop_name) {
      app.toast('请备注商家名称');
      return false;
    }
    this.setData({ post_loading: true });
    api.goUse({
      order_id: this.data.order.id,
      password: this.data.password,
      shopname: this.data.shop_name,
      use_num: this.data.use_num
    }).then((data) => {
      wx.showModal({
        title: '核销成功',
        content: '恭喜您，您已经完成核销！',
        confirmText: '返回',
        success: function (res) {
          wx.switchTab({
            url: '/pages/tabpage/index',
          })
        }
      })
      this.setData({ post_loading: false });
    }).catch((error) => {
      this.setData({ post_loading: false });
      app.toast(error);
    });
  },

  onTextChange: function (e) {
    this.setData({
      remark: e.detail
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})